/********************************************************
 * Filename: SoftTabListener.java
 * Author: LMU
 * Program: TUnit
 * Description: Creates the listener to use soft tabs.  
 * Revision History:
 * 20060515: Initial Revision.
 * *****************************************************/

package edu.lmu.cs.tUnit.editor;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;

import edu.lmu.cs.tUnit.plugin.TUnitPlugin;
import edu.lmu.cs.tUnit.preferences.TclPreferencesConstants;

public class SoftTabListener implements VerifyListener {

	private boolean useSoftTab;
	private String softTab;
	   
	/**
	 * Verify if a tab is used
	 */
	public void verifyText(VerifyEvent evt) {
		if(useSoftTab){
			if(evt.text.equals("\t")){
				evt.text = softTab;
			}
		}
	}

	/**
	 * Preferences have changed.  Update the tabbing information.  
	 * @param event
	 */
	public void preferenceChanged(PropertyChangeEvent event){
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();	
		setUseSoftTab(store.getBoolean(TclPreferencesConstants.P_USE_SPACES_FOR_TABS));
		setSoftTabWidth(store.getInt(TclPreferencesConstants.P_NUMBERSPACES));
	}

	/**
	 * See if a soft tab is to be used
	 * @param useSoftTab
	 */
	public void setUseSoftTab(boolean useSoftTab){
		this.useSoftTab = useSoftTab;
	}

	/**
	 * Width of a soft tab
	 * @param softTabWidth
	 */
	public void setSoftTabWidth(int softTabWidth){
		softTab = "";
		for(int i = 0; i < softTabWidth; i++){
			softTab = softTab + " ";
		}
	}
}
